package ie.lawlor.amvc.swing;

import ie.lawlor.amvc.IDialogEnabledView;
import ie.lawlor.amvc.IView;
import ie.lawlor.amvc.controller.Controller;
import ie.lawlor.amvc.event.Event;
import ie.lawlor.amvc.event.EventName;

import java.awt.Container;

import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import org.apache.log4j.Logger;

public abstract class JMenuView extends javax.swing.JMenu implements SwingView {
    
	protected SwingViewProxy viewProxy;
	protected Logger logger;
	
    protected JMenuView(String name) {
		viewProxy = new SwingViewProxy(name, this);
		logger = viewProxy.getLogger();
		setText(getMenuName());
        addAncestorListener(new AncestorListener(){
            public void ancestorAdded(AncestorEvent e) {
            	viewProxy.setViewVisible(true);
            }
            public void ancestorMoved(AncestorEvent e) {
            }
            public void ancestorRemoved(AncestorEvent e) {
				viewProxy.setViewVisible(false);
            }
        });
    }
    
    protected abstract String getMenuName();
    
	/* (non-Javadoc)
	 * @see com.informatica.amvc.View#setViewVisible(boolean)
	 */
	public void setViewVisible(boolean visible) {
		viewProxy.setViewVisible(visible);
	}

	public void eventIn(Event e) {
		viewProxy.eventIn(e);
	}

	/* (non-Javadoc)
	 * @see com.informatica.amvc.EventSource#fire(com.informatica.amvc.Event)
	 */
	public void fire(Event e) {
		viewProxy.fire(e);
	}

	/* (non-Javadoc)
	 * @see com.informatica.amvc.event.EventSource#fire(com.informatica.amvc.event.EventName, java.lang.Object)
	 */
	public void fire(EventName name, Object payload) {
		viewProxy.fire(name, payload);
	}

	/* (non-Javadoc)
	 * @see com.informatica.amvc.event.EventSource#fire(com.informatica.amvc.event.EventName)
	 */
	public void fire(EventName name) {
		viewProxy.fire(name);
	}

	/* (non-Javadoc)
	 * @see com.informatica.amvc.View#addChild(com.informatica.amvc.View, java.lang.String)
	 */
	public void addChild(IView childView, String viewName) {
		viewProxy.addChild(childView, viewName);
	}

	/* (non-Javadoc)
	 * @see com.informatica.amvc.View#addChild(com.informatica.amvc.View)
	 */
	public void addChild(IView childView) {
		viewProxy.addChild(childView);
	}

	public void addDialogChild(IDialogEnabledView childView) {
		viewProxy.addDialogChild(childView);
	}

	/* (non-Javadoc)
	 * @see com.informatica.amvc.View#setParent(com.informatica.amvc.View)
	 */
	public void setParent(IView view) {
		viewProxy.setParent(view);
	}
	
	/* (non-Javadoc)
	 * @see com.informatica.amvc.View#setParent(com.informatica.amvc.View)
	 */
	public void setParent(IView view, String viewName) {
		viewProxy.setParent(view, viewName);
	}
	
	/* (non-Javadoc)
	 * @see com.informatica.amvc.swing.SwingView#getParentView()
	 */
	public IView getParentView() {
		return viewProxy.getParentView();
	}

	/* (non-Javadoc)
	 * @see com.informatica.amvc.View#setController(com.informatica.amvc.Controller)
	 */
	public void setController(Controller controller) {
		viewProxy.setController(controller);
	}
	
	public Controller getController(){
		return viewProxy.getController();
	}
    
    /* (non-Javadoc)
	 * @see com.informatica.amvc.swing.SwingView#getContainer(java.lang.String)
	 */
	public Container getContainer(String viewName) {
		return this;
	}

	public void doHideView(Event e) {
		//TODO Is there any need to implement this?
	}

	public void doShowView(Event e) {
		//TODO Is there any need to implement this?
	}
	
	public void doInitView(Event e) {
		//TODO Is there any need to implement this?
	}
}
